/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Serum.Form.Module.Fields.Price = Ext.extend(Ext.Panel, {

    // Calculate taxed and untaxed prices
    calculateuntaxed: function(_roundPower) {
        var roundPower = (_roundPower instanceof Number) ? _roundPower : this.roundPower;
        this.untaxedPriceField.setValue(Math.round(this.taxedPriceField.getValue() / 1.196 * roundPower) / roundPower);
    },
    calculatetaxed: function(_roundPower) {
        var roundPower = (_roundPower instanceof Number) ? _roundPower : this.roundPower;
        this.taxedPriceField.setValue(Math.round(this.untaxedPriceField.getValue() * 1.196 * roundPower) / roundPower);
    },
    /**
     * Constructor
     * 
     * @param   {Object}    baseConfig
     */
    constructor: function(baseConfig) {
        // Get parameters
        // returntaxed set to true, returns the taxed value, and returns the untaxed value
        var returntaxed = baseConfig.returntaxed || false;

        this.roundPower = Math.pow(10, parseInt(baseConfig.round || 5));

        // name specifies the field's name in the form
        var name           =  baseConfig.name;
        var value          =  baseConfig.value;
        var textfieldlabel =  baseConfig.fieldLabel;

        var config  = Ext.apply({
            untaxed_price: {
                fieldLabel: textfieldlabel + " (" + __('base~form_label_untaxed_price') + ")"
            } ,
            taxed_price: {
                fieldLabel: textfieldlabel + " (" + __('base~form_label_taxed_price') + ")"
            },
            baseName: baseConfig.name || 'price'
        }, baseConfig);

        var self = this;
        this.untaxedPriceField = new Ext.form.NumberField({
                                        fieldLabel:         config.untaxed_price.fieldLabel,
                                        enableKeyEvents:    true,
                                        decimalPrecision:   2,
                                        labelWidth:         baseConfig.labelWidth || 140,

                                        listeners: {
                                            focus: { fn: function() {
                                                self.taxedPriceField.addClass("disabled-field");
                                                self.untaxedPriceField.removeClass("disabled-field");
                                            }},
                                            keyup: self.calculatetaxed,
                                            scope: self
                                        }
                                    }
                               );

        this.taxedPriceField = new Ext.form.NumberField({
                                    fieldLabel:         config.taxed_price.fieldLabel,
                                    inputType:          'text',
                                    enableKeyEvents:    true,
                                    decimalPrecision:   2,
                                    labelWidth:         baseConfig.labelWidth || 140,

                                    listeners: {
                                        focus: { fn: function() {
                                            self.untaxedPriceField.addClass("disabled-field");
                                            self.taxedPriceField.removeClass("disabled-field");
                                         }},
                                         keyup: self.calculateuntaxed,
                                         scope: self
                                      }
                                  });

         // Set the field name to taxedPriceField if we return a taxed price
         if (returntaxed) {
             this.taxedPriceField.name = name;
             this.taxedPriceField.value = value;
             this.calculateuntaxed(new Number(Math.pow(10, 2)));
             this.taxedPriceField.addClass("disabled-field");
             this.untaxedPriceField.removeClass("disabled-field");

         } else {
            this.untaxedPriceField.name = name;
            this.untaxedPriceField.value = value;
            this.calculatetaxed(new Number(Math.pow(10, 2)));
            this.untaxedPriceField.addClass("disabled-field");
            this.taxedPriceField.removeClass("disabled-field");
        }

        Serum.Form.Module.Fields.Price.superclass.constructor.call(this, {
            border: false,
            layout: 'form',
            items: [
                this.untaxedPriceField,
                this.taxedPriceField
            ],
            labelWidth: baseConfig.labelWidth || 140
        });  
    },

    /**
     * Validate form
     *
     * @param   {String}    value
     */
    validateForm: function(value) {
        return true;
    }
});

Ext.reg('price', Serum.Form.Module.Fields.Price);